<template>
    <div id="member">
        <el-col :span="24" class="action-bar">
            <el-button type="primary" size="medium" @click="signatureVisible=true">添加</el-button>
        </el-col>
        

        <!-- 列表 -->
        <el-table
            :data="tableData"
            style="width: 100%">
            
            <el-table-column
                prop="date"
                label="头像"
                width="100">
                <template slot-scope="scope">
                    <div>
                        <img :src="scope.row.logo_url" alt="" style="width:60px;border-radius:50%;height:60px;object-fit:cover">
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="name"
                label="活动方名称"
                width="200"
                >
            </el-table-column>
            <el-table-column
                prop="type_name"
                label="活动方类型"
                width="200"
                >
            </el-table-column>
            
            <el-table-column
                prop="created_at"
                label="创建时间"
                width="200"
                >
            </el-table-column>

            
            <el-table-column label="操作" width='200'>
                <template slot-scope="scope">
                    <el-button size="mini"  type='primary' @click="editorOwner(scope.row.id)">编辑</el-button>
                    <el-button size="mini" type='danger' @click="deleteOwner(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
            
        </el-table>

		<el-dialog title="活动方" :visible.sync="signatureVisible" @close="initData">
            <el-form label-width="100px">
                <el-form-item label="活动方类型">
                    <el-select v-model="member.ownerTypeOption" placeholder="选择" >
                        <el-option
                            v-for="item in ownerTypeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                            >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="头像" >
                    <el-upload
                        class="avatar-uploader"
                        action="https://wxapp.gdhrma.org.cn/api/file/upload-img"
                        name="photo"
                        :show-file-list="false"
                        :on-success="handlePhoto"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="photoImg" :src="photoImg" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                <el-form-item label="活动方名称">
                    <el-input palceholder="活动方名称" v-model="member.content"></el-input>
                </el-form-item>
                
            </el-form>
            <div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submit" >提交</el-button>
			</div>
		</el-dialog>
    </div>
</template>
<script>
export default {
    name:'member',
    data() {
        return {
            signatureVisible:false,//编辑弹窗
            ownerId:'',//选中的用户id
            tableData: [{status:true}],
            page:1,
            totalPage:0,
            member:{
                content:'',
                ownerTypeOption:''
            },
            ownerTypeOptions:[
                {label:'官方',value:1},
                {label:'非官方',value:0}
            ],
            photoImg:'',
            photoId:'',
        }
    },
    methods: {      
        pageChange(e){
			//翻页
			this.page=e;
			this.getActivePartyList();
        },
        
        async getActivePartyList(){
            let params={
                page:this.page,
                limit:16,
            }
            let res=await this.$api.getActivePartyList(
                params);
            if(res.data.level=='success'){
                this.tableData=res.data.data;
                this.totalPage=res.data.page_info.total;
            }
        },
        async submit(){
            let params={
                id:this.ownerId,
                logo_id:this.photoId,
                name:this.member.content,
                type:this.member.ownerTypeOption
            }
            let res=await this.$api.setActiveParty(params)
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.signatureVisible = false
                this.getActivePartyList()
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
                
            }
        },
        async getActivePartyDetail(id){
            //获取详情
            let res=await this.$api.getActivePartyDetail({id:id});
            if(res.data.level=='success'){
                let data=res.data.data;
                this.member.content=data.name;
                this.photoId=data.logo_id;
                this.photoImg=data.logo_url;
                this.member.ownerTypeOption=+data.type
                this.signatureVisible=true;
                this.ownerId=data.id;
            }else{
                this.$message.error(res.data.message)
            }
        },
        editorOwner(id){
            //编辑
            this.getActivePartyDetail(id)
        },
        async deleteOwner(id){
            let judge=null;
            let tit='删除该活动方?';
            try {
              judge=  await this.$confirm(tit, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
                })
            } catch (error) {
                judge='cancel'
            }
            if(judge=='confirm'){
                this.delActiveParty(id)
            }
            console.log(judge)
        },
        async delActiveParty(id){
            let res =await this.$api.delActiveParty({id:id});
            if(res.data.level=='success'){
                this.$message.success(res.data.message);
                this.getActivePartyList();
            }else{
                this.$message.error(res.data.message)
            }
        },
        beforeAvatarUpload(file) {
			//上传照片前的操作
			// console.log(file,555)
		},
		handlePhoto(res, file) {
			//上传照片成功
			if(res.data){
				this.photoImg=res.data.img_url;
				this.photoId=res.data.id;
			}
        },

        initData(){

        }

    },
    mounted() {
        this.getActivePartyList();
    },
}
</script>
<style lang="scss" scoped>
.screen{
          margin-top: -10px !important;
      }
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
			margin: 10px 0px;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
      }
      .task-form-text{
				color: #666666;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #f2f2f2;
			padding: 8px;
			margin: 10px 0px;
			box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
		.executorList{
			display: flex;
			flex-wrap: wrap;
			div{
				margin:10px;
				border:1px solid #eee;
				border-radius: 5px;
				padding:5px 10px;
			}
		}

        .avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
		border: 1px dashed #d9d9d9;
	}
	.avatar {
		width: 178px;
		height: 178px;
		display: block;
		object-fit: contain;
	}
    .add-table-wrapper{
        display: flex;
        justify-content: center;
        padding:30px 0; 
    }
</style>